package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysAdminUser;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.dto.SysAdminUserDTO;
import com.ruoyi.system.domain.dto.UserResetDTO;
import com.ruoyi.system.service.ISysAdminUserProjectService;
import com.ruoyi.system.service.ISysAdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wux
 * @date 2023/5/11 9:19
 */
@Api(tags = "(运营端) 账号管理")
@RestController
@RequestMapping("/admin/user")
public class SysAdminUserController {
    @Autowired
    private ISysAdminUserService sysAdminUserService;
    @Autowired
    private ISysAdminUserProjectService sysAdminUserProjectService;

    @GetMapping("/list")
    @ApiOperation(value = "用户查询列表", response = SysAdminUser.class)
    public ResultEntity getPointBySection(SysAdminUserDTO.AdminUserSelectDTO params) {
        return ResultEntity.createSuccess(PageUtil.getPageList(sysAdminUserService.listByPage(params)));
    }

    @PostMapping("/add")
    @Log(title = "新增运营端用户", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增用户", response = SysAdminUser.class)
    public ResultEntity add(@Validated @RequestBody SysAdminUserDTO params) {
        //检查账号和手机号是否存在
        List<SysAdminUser> checkList = sysAdminUserService.list(new LambdaQueryWrapper<SysAdminUser>()
                .eq(SysAdminUser::getPhone, params.getPhone()).or()
                .eq(SysAdminUser::getAccount, params.getAccount()));
        if (!CollectionUtils.isEmpty(checkList)) {
            throw new RuntimeException("账号或手机号已存在!");
        }
        SysAdminUser entity = new SysAdminUser();
        BeanUtils.copyProperties(params, entity);
        entity.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        entity.setPassword(SecurityUtils.encryptPassword(entity.getPassword()));
        sysAdminUserService.saveOrUpdate(entity);
        //新增用户项目
        sysAdminUserProjectService.saveList(params.getProjectIdList(), entity.getId());
        return ResultEntity.createSuccess(entity);
    }

    @PostMapping("/update")
    @Log(title = "修改运营端用户", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户", response = SysAdminUser.class)
    public ResultEntity update(@Validated @RequestBody SysAdminUserDTO params) {
        SysAdminUser user = sysAdminUserService.getById(params.getId());
        if (StringUtils.isNotBlank(params.getPassword()) && !params.getPassword().equals(user.getPassword())) {
            params.setPassword(SecurityUtils.encryptPassword(params.getPassword()));
        } else {
            params.setPassword(null);
        }
        SysAdminUser entity = new SysAdminUser();
        BeanUtils.copyProperties(params, entity);
        entity.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        sysAdminUserService.saveOrUpdate(entity);
        //修改用户项目
        sysAdminUserProjectService.saveList(params.getProjectIdList(), entity.getId());
        return ResultEntity.createSuccess(entity);
    }

    @PostMapping("/delete/{id}")
    @Log(title = "删除运营端用户", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除用户", response = SysAdminUser.class)
    public ResultEntity add(@PathVariable Long id) {
        sysAdminUserService.removeById(id);
        return ResultEntity.createSuccess();
    }

    public static void main(String[] args) {
        System.out.println(SecurityUtils.encryptPassword("Yjy123456"));
    }

    @PostMapping("/resert")
    @Log(title = "修改密码", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改密码", response = SysAdminUser.class)
    public ResultEntity add(@Validated @RequestBody UserResetDTO params) {
        //检查用户原密码是否一致
        SysAdminUser adminUser = sysAdminUserService.getById(params.getId());
        if (!SecurityUtils.matchesPassword(params.getPassword(),adminUser.getPassword())) {
            throw new BaseException("原密码输入错误!");
        }
        adminUser.setPassword(SecurityUtils.encryptPassword(params.getNewPassword()));
        sysAdminUserService.updateById(adminUser);
        return ResultEntity.createSuccess();
    }

}
